package com.imooc.aspect;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Macoco on 2017-05-12.
 */
@Aspect
@Component
public class HttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    /*    @Before("execution(public * com.imooc.controller.GirlController.*(..))")
        public void log() {
            System.out.println("...........................1111111111111111111");
        }
        @After("execution(public * com.imooc.controller.GirlController.*(..))")
        public void doAfter(){
            System.out.println("after3333333333333333333.....");
        }*/
    @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log() {

    }


    @Before("log()")
    public void doBefor(JoinPoint joinpoint) {//注意：joinpint在java.lang
        logger.info("Befor logger1111111111111111111111111111111");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();

        //url
        logger.info("url={}", request.getRequestURL());
        // method
        logger.info("method={}", request.getMethod());
        // ip
        logger.info("ip", request.getRemoteAddr());
        // 类方法
        logger.info("class_method{}",joinpoint.getSignature().getDeclaringTypeName()+"."+joinpoint.getSignature().getDeclaringTypeName());
        // 参数
        logger.info("args={}",joinpoint.getArgs());


    }

    @After("log()")
    public void doAfter() {
        logger.info("After logger22222222222222222222222222222222222");
        //        System.out.println("after3333333333333333333.....");
    }
}

